package com.hjs.bean;

import com.hjs.mapper.StandardMapper;
import com.hjs.tools.Mapper;
import com.hjs.tools.asphalt;
import lombok.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

/**
 * 道路设施路面损害情况记录表
 *
 * @author violetfreesia
 * @date 2020/06/16
 * @author ShenXiaojian
 * @date 2020/6/22 补充
 */
@NoArgsConstructor
@AllArgsConstructor
@Getter
@Setter
@ToString
@Slf4j
public class PavementDamage {
    //损害记录编号
    private Integer damageID;
    //共有属性ID
    private Integer pavementDamageComID;
    //损害类型ID
    private Integer damageTypeID;
    //损坏长
    private Double damageLength;
    //损坏宽
    private Double damageWidth;
    //损坏高
    private Double damageHeight;
    //损坏面积
    private Double damageArea;
    //损坏密度
    private Double damageDensity;
    //损坏位置及损坏情况描述(place and condition)
    private String damagePAC;
    //备注
    private String remarks;
    //单项得分值
    private Double individualScore;

    public PavementDamage(Integer damageID, PavementDamageCom pavementDamageCom, Integer damageTypeID, Double damageLength, Double damageWidth, Double damageHeight, String damagePAC, String remarks) {
        this.damageID = damageID;
        this.pavementDamageComID = pavementDamageCom.getPavementDamageComID();
        this.damageTypeID = damageTypeID;
        this.damageLength = damageLength;
        this.damageWidth = damageWidth;
        this.damageHeight = damageHeight;
        getDamageArea(this.damageTypeID,this.damageLength,this.damageWidth);
        getDamageDensity(this.damageArea,pavementDamageCom);
        this.damagePAC = damagePAC;
        this.remarks = remarks;
        getScoreByDamageDensity(this.damageTypeID, this.damageDensity);
    }

    private void getScoreByDamageDensity(Integer damageTypeId,Double damageDensity) {
        try (SqlSession sqlSession = Mapper.getSqlSession()) {
            StandardMapper standardMapper = sqlSession.getMapper(StandardMapper.class);
            List<DamageStandard> allDamageStandard = standardMapper.getAllOnePointMinusByDamageTypeId(damageTypeId);
            /*
             * 遍历allDamageStandard列表
             * 若损坏标准匹配
             * 且当前密度夹在某两个中间，则找到所需单项扣分循环结束
             * 注：损坏密度夹在两个扣分标准之间时，取小的那个密度对应的扣分标准扣分
             */
            if (damageDensity < 0.01) {
                this.individualScore = 0.0;
            } else if (damageDensity == 100) {
                this.individualScore = allDamageStandard.get(5).getPointMinus();
            } else {
                for (int i = 0; i < allDamageStandard.size() - 1; i++) {
                    DamageStandard downDamageStandard = allDamageStandard.get(i);
                    DamageStandard upDamageStandard = allDamageStandard.get(i + 1);
                    if (damageDensity >= downDamageStandard.getDamageDensity()
                            && damageDensity < upDamageStandard.getDamageDensity()) {
                        this.individualScore = downDamageStandard.getPointMinus();
                        break;
                    }
                }
            }
        } catch (Exception e) {
            log.error(e.toString());
        }
    }

    private void getDamageArea(Integer damageTypeId, Double damageLength, Double damageWidth) {
        this.damageArea = asphalt.all(damageTypeID,damageLength,damageWidth);
    }

    private void getDamageDensity(Double damageArea,PavementDamageCom pavementDamageCom) {
        this.damageDensity = damageArea/pavementDamageCom.getInspectionArea();
    }
}
